//
//  HLHomeLoveBellView.m
//  YueYue
//
//  Created by hulu001 on 2023/8/14.
//

#import "HLHomeLoveBellView.h"
#import "HLUserOptionRequest.h"
#import "HLHomeListModel.h"
#import "HLSystemRingTool.h"

@interface HLHomeLoveBellView ()

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) QMUIButton *closeButton;

@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) QMUIButton *todayButton;
@property (nonatomic, strong) UIImageView *firstView;

@property (nonatomic, strong) UIImageView *coverView;
@property (nonatomic, strong) UILabel *nicknameLabel;
@property (nonatomic, strong) UIImageView *priceIcon;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *unitLabel;
@property (nonatomic, strong) UILabel *discountLabel;

@property (nonatomic, strong) QMUIButton *rechangeButton;
@property (nonatomic, strong) QMUIButton *callButton;

@property (nonatomic, strong) HLCommonCallbackBoolBlock closeCallback;
@property (nonatomic, strong) HLHomeListModel *model;

@end

@implementation HLHomeLoveBellView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView
{
    self.containerView = [[UIView alloc] init];
    _containerView.layer.masksToBounds = YES;
    _containerView.layer.cornerRadius = 16;
    _containerView.backgroundColor = UIColorWhite;
    [self addSubview:_containerView];
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(300, 400));
    }];
    
    self.closeButton = [[QMUIButton alloc] init];
    [_closeButton setImage:UIImageMake(@"love_bell_close") forState:UIControlStateNormal];
    [self addSubview:_closeButton];
    [_closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.mas_equalTo(0);
    }];
    
    
    self.avatarView = [[UIImageView alloc] init];
    _avatarView.contentMode = UIViewContentModeScaleAspectFill;
    [_containerView addSubview:_avatarView];
    [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.todayButton = [[QMUIButton alloc] init];
    [_todayButton setImage:UIImageMake(@"love_bell_unselected") forState:UIControlStateNormal];
    [_todayButton setImage:UIImageMake(@"love_bell_selected") forState:UIControlStateSelected];
    [_todayButton setTitle:@"今日不再弹出" forState:UIControlStateNormal];
    [_todayButton setTitle:@"今日不再弹出" forState:UIControlStateSelected];
    [_todayButton setTitleColor:[UIColor qmui_colorWithHexString:@"#595A69"] forState:UIControlStateNormal];
    [_todayButton setTitleColor:[UIColor qmui_colorWithHexString:@"#595A69"] forState:UIControlStateSelected];
    [_todayButton addTarget:self action:@selector(todayAction:) forControlEvents:UIControlEventTouchUpInside];
    _todayButton.titleLabel.font = UIFontMake(12);
    _todayButton.selected = [HLHelper loveBellToday];
    _todayButton.imagePosition = QMUIButtonImagePositionLeft;
    _todayButton.spacingBetweenImageAndTitle = 4;
    [_containerView addSubview:_todayButton];
    [_todayButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(12);
    }];
    
    
    self.firstView = [[UIImageView alloc] initWithImage:UIImageMake(@"love_bell_first")];
    _firstView.hidden = YES;
    [_containerView addSubview:_firstView];
    [_firstView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(0);
    }];
    
    self.coverView = [[UIImageView alloc] initWithImage:UIImageMake(@"home_flow_cover")];
    [_containerView addSubview:_coverView];
    [_coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(144);
    }];
    
    self.rechangeButton = [[QMUIButton alloc] init];
    [_rechangeButton setImage:UIImageMake(@"love_bell_rechange") forState:UIControlStateNormal];
    [_rechangeButton setTitle:@"换一个" forState:UIControlStateNormal];
    [_rechangeButton setTitleColor:HLTextColor1 forState:UIControlStateNormal];
    [_rechangeButton.titleLabel setFont:UIFontMake(15)];
    [_rechangeButton addTarget:self action:@selector(requestData) forControlEvents:UIControlEventTouchUpInside];
    _rechangeButton.imagePosition = QMUIButtonImagePositionLeft;
    _rechangeButton.spacingBetweenImageAndTitle = 4;
    _rechangeButton.cornerRadius = 25;
    _rechangeButton.backgroundColor = UIColorWhite;
    [_containerView addSubview:_rechangeButton];
    [_rechangeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(132, 50));
        make.bottom.mas_equalTo(-30);
        make.left.mas_equalTo(12);
    }];
    
    self.callButton = [[QMUIButton alloc] init];
    [_callButton setImage:UIImageMake(@"love_bell_call") forState:UIControlStateNormal];
    [_callButton setTitle:@"接听视频" forState:UIControlStateNormal];
    [_callButton setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [_callButton.titleLabel setFont:UIFontMake(15)];
    _callButton.imagePosition = QMUIButtonImagePositionLeft;
    _callButton.spacingBetweenImageAndTitle = 4;
    _callButton.cornerRadius = 25;
    _callButton.backgroundColor = HLMainColor;
    [_callButton addTarget:self action:@selector(callAction) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:_callButton];
    [_callButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.bottom.equalTo(_rechangeButton);
        make.right.mas_equalTo(-12);
    }];
    
    
    self.priceIcon = [[UIImageView alloc] initWithImage:UIImageMake(@"home_flow_coin")];
    [_containerView addSubview:_priceIcon];
    [_priceIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.bottom.equalTo(_rechangeButton.mas_top).offset(-16);
        make.size.mas_equalTo(16);
    }];
    
    self.priceLabel = [[UILabel alloc] qmui_initWithFont:UIFontBoldMake(14) textColor:UIColorWhite];
    _priceLabel.text = @"199";
    [_containerView addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_priceIcon.mas_right).offset(4);
        make.centerY.equalTo(_priceIcon);
    }];
    
    self.unitLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(8) textColor:[UIColorWhite colorWithAlphaComponent:0.7]];
    _unitLabel.text = @"/分钟";
    [_containerView addSubview:_unitLabel];
    [_unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_priceLabel);
        make.left.equalTo(_priceLabel.mas_right).offset(2);
    }];
    
    self.discountLabel = [[UILabel alloc] qmui_initWithFont:UIFontMake(8) textColor:[UIColorWhite colorWithAlphaComponent:0.7]];
    [_containerView addSubview:_discountLabel];
    [_discountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_unitLabel);
        make.left.equalTo(_unitLabel.mas_right).offset(2);
    }];
    
    self.nicknameLabel = [[UILabel alloc] qmui_initWithFont:UIFontMediumMake(17) textColor:UIColorWhite];
    _nicknameLabel.text = @"Tome";
    [_containerView addSubview:_nicknameLabel];
    [_nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_priceIcon);
        make.bottom.equalTo(_priceIcon.mas_top).offset(-8);
        make.right.mas_equalTo(-12);
    }];
}

- (void)todayAction:(QMUIButton *)btn
{
    btn.selected = !btn.selected;
    [HLHelper toggleLoveBellToday];
}


- (void)requestData
{
    HLUserOptionRequest *loveBellRequest = [[HLUserOptionRequest alloc] initWithType:HLUserOptionTypeLoveBell params:@{}];
    [loveBellRequest startWithCompletionBlockWithSuccess:^(__kindof HLUserOptionRequest * _Nonnull request) {
        if (request.isSuccess){
            NSDictionary *data = [request.result valueForKey:@"data"];
            self.model = [HLHomeListModel mj_objectWithKeyValues:data];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
}

- (void)callAction
{
    if (self.closeCallback){
        self.closeCallback(YES);
    };
    HLUserInfoModel *user = [[HLUserInfoModel alloc] init];
    user.userId = self.model.userId;
    user.avatar = self.model.avatar;
    user.videoType = [_model videoType];
    [HLUIHelper callWithUser:user];
}

- (void)setModel:(HLHomeListModel *)model
{
    _model = model;
    
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[HLUIHelper avatarBigPlaceholder]];
    _nicknameLabel.text = model.nickName;
    _priceLabel.text = model.videoPrice;
    _unitLabel.text = @"/分钟";
    if (model.discountedPrice.length > 0){
        _unitLabel.text = @"/分钟 |";
        NSDictionary * centerAttribtDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle],NSForegroundColorAttributeName:[UIColorWhite colorWithAlphaComponent:0.7], NSFontAttributeName:UIFontMake(8)};
        NSMutableAttributedString *centerAttr = [[NSMutableAttributedString alloc] initWithString:model.discountedPrice attributes:centerAttribtDic];
        _discountLabel.attributedText = centerAttr;
    }
}

+ (void)showWithModel:(HLHomeListModel *)model
{
    [HLRunTime shareInstance].isShowLoveBell = YES;
    [HLSystemRingTool.shareInstance finishPlay];
    [HLSystemRingTool.shareInstance playLoveBell];
    
    CGFloat height = 472;
    HLHomeLoveBellView *contentView = [[HLHomeLoveBellView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
    
    QMUIModalPresentationViewController *modalViewController = [[QMUIModalPresentationViewController alloc] init];

    modalViewController.contentView = contentView;
    modalViewController.didHideByDimmingViewTappedBlock = ^{
        [HLSystemRingTool.shareInstance finishPlay];
        [HLRunTime shareInstance].isShowLoveBell = NO;
    };
    contentView.model = model;
    contentView.closeCallback = ^(BOOL isClose) {
        [HLSystemRingTool.shareInstance finishPlay];
        [HLRunTime shareInstance].isShowLoveBell = NO;
        [modalViewController hideWithAnimated:YES completion:nil];
    };

    [modalViewController showWithAnimated:YES completion:nil];
    
    [contentView.closeButton setQmui_tapBlock:^(__kindof UIControl *sender) {
        [modalViewController hideWithAnimated:YES completion:nil];
        [HLSystemRingTool.shareInstance finishPlay];
        [HLRunTime shareInstance].isShowLoveBell = NO;
    }];
}


+ (void)bell
{
    if (!([[QMUIHelper visibleViewController] isKindOfClass:NSClassFromString(@"HLHomeViewController")]
        || [[QMUIHelper visibleViewController] isKindOfClass:NSClassFromString(@"HLSquareViewController")]
        || [[QMUIHelper visibleViewController] isKindOfClass:NSClassFromString(@"HLMessageViewController")]
        || [[QMUIHelper visibleViewController] isKindOfClass:NSClassFromString(@"HLMineViewController")])){
        return;
    }
    
    if([HLRunTime shareInstance].isShowLoveBell){
        return;
    }
    
    [HLRunTime shareInstance].loveBellCount += 1;
    if ([HLRunTime shareInstance].loveBellCount > 200){
        [HLRunTime shareInstance].loveBellCount = 0;
        
        if (![HLHelper loveBell]){
            return;
        }
        
        if ([HLHelper loveBellToday]){
            return;
        }
        
        HLUserOptionRequest *loveBellRequest = [[HLUserOptionRequest alloc] initWithType:HLUserOptionTypeLoveBell params:@{}];
        [loveBellRequest startWithCompletionBlockWithSuccess:^(__kindof HLUserOptionRequest * _Nonnull request) {
            if (request.isSuccess){
                
                HLHomeListModel *user;
                
                NSDictionary *data = [request.result valueForKey:@"data"];
                user = [HLHomeListModel mj_objectWithKeyValues:data];
                if (user) {
                    [self showWithModel:user];
                }
                
            }
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            
        }];
        
    }
}
@end
